package com.example.biji.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.biji.adapter.NoteAdapter;
import com.example.biji.dao.NoteDao;
import com.example.biji.model.Note;
import com.example.biji.R;

import java.util.List;

public class MainActivity extends AppCompatActivity implements NoteAdapter.OnItemClickListener {
    private RecyclerView rvNotes;
    private NoteAdapter noteAdapter;
    private List<Note> noteList;
    private NoteDao noteDao;
    private int userId;
    private String username;
    private TextView tvWelcome;
    private static final int REQUEST_CODE_SETTINGS = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userId = getIntent().getIntExtra("userId", -1);
        username = getIntent().getStringExtra("username");

        if (userId == -1) {
            Toast.makeText(this, "用户ID获取失败", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        tvWelcome = findViewById(R.id.tv_welcome);
        tvWelcome.setText("欢迎回来，" + username);

        initViews();
        loadNotes();
        setupRecyclerView();
        setupAddButton();
        setupSettingsButton();
        setupSearchButton();
    }

    private void initViews() {
        rvNotes = findViewById(R.id.rv_notes);
    }

    private void loadNotes() {
        noteDao = new NoteDao(this);
        noteList = noteDao.queryAllNotes(userId);
    }

    private void setupRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvNotes.setLayoutManager(layoutManager);

        noteAdapter = new NoteAdapter(noteList);
        noteAdapter.setOnItemClickListener(this);
        rvNotes.setAdapter(noteAdapter);
    }

    private void setupAddButton() {
        findViewById(R.id.iv_add_note).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddNoteActivity.class);
                intent.putExtra("userId", userId);
                startActivity(intent);
            }
        });
    }

    private void setupSettingsButton() {
        findViewById(R.id.iv_settings).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, UserSettingsActivity.class);
                intent.putExtra("userId", userId);
                intent.putExtra("username", username);
                startActivityForResult(intent, REQUEST_CODE_SETTINGS);
            }
        });
    }

    private void setupSearchButton() {
        findViewById(R.id.iv_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, NoteSearchActivity.class);
                intent.putExtra("userId", userId);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshNotes();
    }

    private void refreshNotes() {
        noteList = noteDao.queryAllNotes(userId);
        noteAdapter.setNotes(noteList);
        noteAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int position) {
        Note note = noteList.get(position);
        Intent intent = new Intent(this, NoteDetailActivity.class);
        intent.putExtra("noteId", note.getId());
        intent.putExtra("userId", userId);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SETTINGS && resultCode == RESULT_OK) {
            if (data != null) {
                username = data.getStringExtra("username");
                tvWelcome.setText("欢迎回来，" + username);
            }
        }
    }
}